<template>
  <div class="main-layout">
    <!-- 左侧导航栏 -->
    <div class="sidebar">
      <div class="logo">
        <div class="logo-icon">
          <img src="@/images/logo.jpg" alt="Logo">
        </div>
      </div>
      <nav class="nav-menu">
        <router-link to="/pose-feasibility" class="nav-item" :class="{ active: $route.path === '/pose-feasibility' }">
          <div class="nav-icon">🏠</div>
          <div class="nav-text">位姿可行域推算</div>
        </router-link>
        <router-link to="/target-optimization" class="nav-item"
          :class="{ active: $route.path === '/target-optimization' }">
          <div class="nav-icon">⚙️</div>
          <div class="nav-text">靶点布设优化</div>
        </router-link>
        <router-link to="/tolerances-design" class="nav-item" :class="{ active: $route.path === '/tolerances-design' }">
          <div class="nav-icon">📊</div>
          <div class="nav-text">容差设计</div>
        </router-link>
      </nav>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 顶部标题栏 -->
      <div class="top-header">
        <div class="header-left">
          <h1 class="page-title">{{ currentPageTitle }}</h1>
        </div>
        <div class="header-tabs">
          <div class="segmented-control">
            <div class="segment" :class="{
              active: selectedSegment === 'start',
              disabled: isCalculating
            }" @click="(event) => selectSegment('start', event)">
              <span class="segment-icon">▶️</span>
              <span class="segment-text">开始计算</span>
            </div>
            <div class="segment" v-if="route.path === '/target-optimization'" :class="{
              active: selectedSegment === 'visualize',
              disabled: !calculationCompleted || isCalculating
            }" @click="(event) => selectSegment('visualize', event)">
              <span class="segment-icon">📈</span>
              <span class="segment-text">可视化计算</span>
            </div>
            <div class="segment" :class="{
              active: selectedSegment === 'download',
              disabled: !calculationCompleted || isCalculating
            }" @click="(event) => selectSegment('download', event)">
              <span class="segment-icon">📥</span>
              <span class="segment-text">下载结果</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 内容区域 -->
      <div class="content-area">
        <router-view v-slot="{ Component }">
          <component :is="Component" ref="routerViewRef" />
        </router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { useCalculationStore } from '@/stores/calculation'

const route = useRoute()
const routerViewRef = ref(null)
const calculationStore = useCalculationStore()

// 根据当前路由动态显示页面标题
const currentPageTitle = computed(() => {
  const titleMap = {
    '/pose-feasibility': '位姿可行域推算',
    '/target-optimization': '靶点布设优化',
    '/tolerances-design': '容差设计'
  }
  return titleMap[route.path] || '测点计算'
})

// 分段控制器状态管理
const selectedSegment = ref('start') // 默认选中开始计算

// 从store获取计算状态
const isCalculating = computed(() => calculationStore.isCalculating)
const calculationCompleted = computed(() => calculationStore.calculationCompleted)

const selectSegment = async (segment, event) => {
  // 如果点击的是禁用的按钮，直接返回
  if (segment === 'download' && (!calculationCompleted.value || isCalculating.value)) {
    return
  }

  if (segment === 'start' && isCalculating.value) {
    return
  }

  if (segment === 'visualize' && (!calculationCompleted.value || isCalculating.value)) {
    return
  }

  // 添加点击效果
  const segmentElement = event?.target?.closest('.segment')
  if (segmentElement) {
    segmentElement.classList.add('clicked')
    setTimeout(() => {
      segmentElement.classList.remove('clicked')
    }, 200)
  }

  selectedSegment.value = segment
  if (segment === 'start') {
    // 开始计算 根据当前路由 调用不同的计算方法
    // 等待DOM更新
    await nextTick()

    // 获取计算类型
    let calculationType = ''
    if (route.path === '/pose-feasibility') {
      calculationType = 'pose-feasibility'
      // 调用位姿可行域推算的计算方法
      if (routerViewRef.value && typeof routerViewRef.value.startCalculate === 'function') {
        calculationStore.startCalculation(calculationType)
        routerViewRef.value.startCalculate()
      }
    } else if (route.path === '/target-optimization') {
      calculationType = 'target-optimization'
      // 调用靶点布设优化的计算方法
      if (routerViewRef.value && typeof routerViewRef.value.startOptimize === 'function') {
        calculationStore.startCalculation(calculationType)
        routerViewRef.value.startOptimize()
      }
    } else if (route.path === '/tolerances-design') {
      calculationType = 'tolerances-design'
      // 调用容差设计的计算方法
      if (routerViewRef.value && typeof routerViewRef.value.startDesign === 'function') {
        calculationStore.startCalculation(calculationType)
        routerViewRef.value.startDesign()
      }
    }
  } else if (segment === 'visualize') {
    // 可视化计算
    await nextTick()

    if (route.path === '/target-optimization') {
      // 调用靶点布设优化的可视化计算方法
      if (routerViewRef.value && typeof routerViewRef.value.handleVisualizeCalculation === 'function') {
        await routerViewRef.value.handleVisualizeCalculation()
      } else {
        console.error('子页面没有 handleVisualizeCalculation 方法')
      }
    }
  } else if (segment === 'download') {
    // 下载结果
    if (calculationStore.canDownload()) {
      downloadResults()
    }
  }
}

// 下载结果函数
const downloadResults = async () => {
  console.log('开始下载结果')

  // 等待DOM更新
  await nextTick()

  // 根据当前路由调用对应子页面的下载方法
  if (route.path === '/pose-feasibility') {
    // 调用位姿可行域推算的下载方法
    if (routerViewRef.value && typeof routerViewRef.value.downloadResults === 'function') {
      await routerViewRef.value.downloadResults()
    } else {
      console.error('子页面没有 downloadResults 方法')
    }
  } else if (route.path === '/target-optimization') {
    // 调用靶点布设优化的下载方法
    if (routerViewRef.value && typeof routerViewRef.value.downloadResults === 'function') {
      await routerViewRef.value.downloadResults()
    } else {
      console.error('子页面没有 downloadResults 方法')
    }
  } else if (route.path === '/tolerances-design') {
    // 调用容差设计的下载方法
    if (routerViewRef.value && typeof routerViewRef.value.downloadResults === 'function') {
      await routerViewRef.value.downloadResults()
    } else {
      console.error('子页面没有 downloadResults 方法')
    }
  }
}
</script>

<style scoped>
.main-layout {
  display: flex;
  height: 100vh;
  background-color: #1a1a1a;
  color: #ffffff;
}

.sidebar {
  width: 200px;
  background-color: #2d2d2d;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
  margin: 20px;
  /* border-radius: 20px; */
}

.logo {
  margin-bottom: 30px;
}

.logo-icon {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.logo-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

.nav-menu {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  align-items: center;
}

.nav-item {
  width: 160px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  border-radius: 12px;
  text-decoration: none;
  transition: all 0.3s ease;
  position: relative;
  padding: 0 15px;
  gap: 12px;
}

.nav-item:hover {
  background-color: #3a3a3a;
}

.nav-item.active {
  background-color: #485FD4;
  box-shadow: 0 0 20px rgba(74, 144, 226, 0.3);
}

.nav-icon {
  font-size: 20px;
  flex-shrink: 0;
}

.nav-text {
  font-size: 14px;
  font-weight: 500;
  color: #ffffff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #1a1a1a;
}

.top-header {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
  /* border-bottom: 1px solid #3a3a3a; */
}


.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #ffffff;
  margin: 0;
}

.header-tabs {
  display: flex;
  flex: 1;
  justify-content: center;
}

.segmented-control {
  display: flex;
  background-color: #2d2d2d;
  border-radius: 32px;
  padding: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.segment {
  padding: 12px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #b0b0b0;
  font-weight: 500;
  font-size: 14px;
  text-align: center;
  position: relative;
  border-radius: 32px;
  min-width: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background-color: transparent;
}

.segment-icon {
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.segment-text {
  font-size: 14px;
  font-weight: 500;
  color: inherit;
}

.segment.active {
  background-color: #485FD4;
  ;
  color: #ffffff;
}

.segment:hover:not(.active) {
  background-color: rgba(255, 255, 255, 0.05);
  color: #ffffff;
}

.segment.clicked {
  transform: scale(0.95);
  background-color: rgba(72, 95, 212, 0.8);
  box-shadow: 0 0 20px rgba(72, 95, 212, 0.4);
  transition: all 0.1s ease;
}

.segment.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
  /* color: #666; */
}

.segment.disabled:hover {
  background-color: transparent;
  color: #666;
}


.content-area {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
  background-color: #1a1a1a;
  padding-top: 0px;
}
</style>